package com.abe.app;

import com.abe.clickeffect.ClickEffectTool;

import android.app.AlertDialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * @see 界面适配框架
 * */
public class AdaptiveAlertDialog extends AlertDialog {

	protected Context context;
	protected static float scale = 0;
	private static final float UI_STANDARD_width = 1280;

	protected AdaptiveAlertDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
	}

	protected AdaptiveAlertDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
	}

	protected AdaptiveAlertDialog(Context context) {
		super(context);
		this.context = context;
	}

	@Override
	public void setContentView(int layoutResID) {
		View view = View.inflate(context, layoutResID, null);
		this.setContentView(view);
	}

	@Override
	public void setContentView(View view) {
		if (scale == 0) {
			initScreenScale();
		}
		if (scale != 1) {
			RelayoutTool.relayoutViewHierarchy(view, scale);
			ClickEffectTool.unifiedAddClickEffect(view);
		}
		super.setContentView(view);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (scale == 0) {
			initScreenScale();
		}
		RelayoutTool.relayoutViewHierarchy(view, scale);
		RelayoutTool.scaleLayoutParams(params, scale);
		ClickEffectTool.unifiedAddClickEffect(view);
		super.setContentView(view, params);
	}

	public void initScreenScale() {
		DisplayMetrics displayMetrics = ManagerAPP.getContext().getResources()
				.getDisplayMetrics();
		float width = displayMetrics.widthPixels;
		scale = width / UI_STANDARD_width;
	}

}
